package com.epoint.mobileoa.ui.personchoose;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.db.NewDbUtil;
import com.epoint.mobileoa.ui.personchoose.model.PersonChooseInfoModel;
import com.epoint.mobileoa.ui.wsgw.model.UserInfoModel;

/**
 * @author liyc
 * @time 2012-12-19 上午10:23:53
 * @annotation 新人员选择
 */
public class NewPersonChooseView extends SuperView {

	private ListView lv;
	private List<PersonChooseInfoModel> plist;
	private LvAdapter adapter;
	private ArrayList<String> backflaglist = new ArrayList<String>();// 用作返回按钮作标记
	private ArrayList<String> choosePersonList = new ArrayList<String>();
	private EditText etPerson,etTj;
	private Button btSearch,btConfirm,btClean;
	private NewDbUtil dbUtil;

	/**
	 * 从哪个模块跳转过来的
	 */
	public final static String Which_View = "Which_view";

	private boolean filterGuidsflag;
	private String filterGuids;

	private RelativeLayout pc1_ll_cz;

	@Override
	public void initControls() {
		pc1_ll_cz = (RelativeLayout) findViewById(R.id.pc1_ll_cz);
		btConfirm = (Button) findViewById(R.id.btConfirm_pc);
		btConfirm.setOnClickListener(this);
		btClean= (Button) findViewById(R.id.btClean_pc);
		btClean.setOnClickListener(this);
		etTj = (EditText) findViewById(R.id.et_cz1);
		btSearch = (Button) findViewById(R.id.bt_cz1);
		btSearch.setOnClickListener(this);
		etPerson = (EditText) findViewById(R.id.et_pcselected);
		top_left_bt.setOnClickListener(this);
		dbUtil = new NewDbUtil(this);
		lv = (ListView) findViewById(R.id.lvContacts);
		lv.setOnItemClickListener(this);




		String passedNames = getIntent().getStringExtra("names");
		String passedGuids = getIntent().getStringExtra("guids");
		etPerson.setText(passedNames);
		String[] guids = passedGuids.split(";");
		for(String s:guids)
		{
			choosePersonList.add(s);
		}

		filterGuids = getIntent().getStringExtra("filterGuids");
		if(filterGuids!=null){
			if(filterGuids.split(";").length>0)
			{
				filterGuidsflag = true;
				pc1_ll_cz.setVisibility(View.GONE);
				plist = dbUtil.getFilterListByUserguids(this, filterGuids);

			}
			else{
				plist = dbUtil.getPersonChoose("");
			}
		}
		else{
			plist = dbUtil.getPersonChoose("");
		}
		adapter = new LvAdapter();
		lv.setAdapter(adapter);
	}

	@Override
	public void onClick(View v){
		if(v==top_left_bt)
		{
			backLogic();
		}
		else if(v==btSearch)
		{
			InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(etTj.getWindowToken(), 0);

			String tj = etTj.getText().toString().trim();
			if(tj.equals("")){
				etTj.setText("");
				return;
			}
			plist = dbUtil.searchPersonChoose(tj);
			adapter.notifyDataSetChanged();
		}
		else if(v==btConfirm)
		{
			choosedDeal();
		}
		else if(v==btClean){
			etPerson.setText("");
			backflaglist.clear();
			choosePersonList.clear();
			if(filterGuidsflag){
				plist = dbUtil.getPersonChoose(filterGuids);
			}
			else{
				plist = dbUtil.getPersonChoose("");
			}
			adapter.notifyDataSetChanged();
		}
	}



	private void choosedDeal()
	{
		try{
			String guids = "";
			for(String s:choosePersonList)
			{
				if(!s.equals("")){
					guids += s+";";
				}
			}
			String whichview = getIntent().getStringExtra(Which_View);
			Intent intent = new Intent(this,Class.forName(whichview));   
			intent.putExtra("names", etPerson.getText().toString());
			intent.putExtra("guids", guids);
			setResult(RESULT_OK, intent);   
			finish(); 
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		PersonChooseInfoModel p = plist.get(position);
		if(p.type.equals("部门"))
		{
			backflaglist.add(p.guid);
			plist = dbUtil.getPersonChoose(p.guid);
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void refresh(Object... param) throws Exception {

	}

	@Override
	public void KEYCODE_BACK() {
		backLogic();
	}

	public void backLogic() {
		/**
		 * 返回按钮操作逻辑 liyc 2011.08.02
		 */
		if (backflaglist.size() == 0) {
			choosedDeal();
		} else if (backflaglist.size() == 1) {
			plist = dbUtil.getPersonChoose("");
			adapter.notifyDataSetChanged();
		} else {
			plist = dbUtil.getPersonChoose(backflaglist.get(backflaglist.size() - 2));
			adapter.notifyDataSetChanged();
		}
		if (backflaglist.size() > 0) {
			backflaglist.remove(backflaglist.size() - 1);
		}
	}




	public static void choosePerson(SuperView con,TextView tv,int requestCode)
	{
		Intent intent = new Intent(con, NewPersonChooseView.class);
		intent.putExtra(Which_View, con.getClass().getName());
		intent.putExtra("names", tv.getText().toString());
		intent.putExtra("guids", tv.getTag()==null?"":tv.getTag().toString());
		con.startActivityForResult(intent,requestCode);
	}

	//自带过滤人范围的人员选择
	public static void choosePersonFilter(SuperView con,TextView tv,int requestCode,String filterGuids)
	{
		Intent intent = new Intent(con, NewPersonChooseView.class);
		intent.putExtra(Which_View, con.getClass().getName());
		intent.putExtra("names", tv.getText().toString());
		intent.putExtra("guids", tv.getTag()==null?"":tv.getTag().toString());
		intent.putExtra("filterGuids", filterGuids);
		con.startActivityForResult(intent,requestCode);
	}

	public static void getData(Intent data,TextView tv)
	{
		tv.setTag(data.getStringExtra("guids")==null?"":data.getStringExtra("guids"));
		tv.setText(data.getStringExtra("names"));
	}

	class LvAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return plist.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater mInflater = LayoutInflater.from(getContext());
			ViewHolder holder = new ViewHolder();
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.listviewwithbtnandimg, null);
				holder.img = (ImageView) convertView.findViewById(R.id.img);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.info = (TextView) convertView.findViewById(R.id.info);
				holder.cbPerson = (CheckBox) convertView
						.findViewById(R.id.cbPerson);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final PersonChooseInfoModel p = plist.get(position);
			if(p.type.equals("部门")){
				holder.img.setBackgroundResource(R.drawable.contacts_ou);
				holder.title.setTextColor(getResources().getColor(R.color.black));
				holder.cbPerson.setVisibility(View.GONE);
			}
			else if(p.type.startsWith("人员:"))
			{
				holder.title.setTextColor(getResources().getColor(R.color.blue));
				holder.img.setBackgroundResource(R.drawable.contackt_user);
				holder.cbPerson.setVisibility(View.VISIBLE);
			}
			holder.title.setText(p.name);
			holder.info.setTextColor(getResources().getColor(R.color.green));
			holder.info.setText(p.type);

			holder.cbPerson.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if(p.type.startsWith("人员:")){
						String userguid = p.guid;
						if(isChecked)
						{
							if(!choosePersonList.contains(userguid))
							{
								choosePersonList.add(userguid);
							}
						}
						else{
							if(choosePersonList.contains(userguid)){
								choosePersonList.remove(userguid);
							}
						}
						etPerson.setText(dbUtil.getUserDisplayNamesByGuids(choosePersonList));
					}
				}
			});

			if(choosePersonList.contains(p.guid))
			{
				holder.cbPerson.setChecked(true);
			}
			else{
				holder.cbPerson.setChecked(false);
			}

			return convertView;
		}

	}

	class ViewHolder {
		public ImageView img;
		public TextView title;
		public TextView info;
		public CheckBox cbPerson;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}

	@Override
	public int getLayoutInt() {
		return R.layout.personchoose1;
	}

	@Override
	public String setTitle() {
		return "人员选择";
	}
}
